package com.kitty.game.gm.handler;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.ServerService;
import com.kitty.game.admin.service.AdminService;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.gm.GmService;
import com.kitty.game.role.model.Role;
import com.kitty.game.utils.TimeUtil;
import com.kitty.mina.cache.SessionUtils;
import com.kitty.mina.message.MessagePusher;
import org.apache.mina.core.session.IoSession;

import java.util.concurrent.TimeUnit;

public class GmFenbi implements GMFather {
    @Override
    public boolean handle(Role aaa, String content) {
        String[] strings = content.split("##");
        String name = strings[0];
        GmService gmService = SpringUtils.getBean(GmService.class);
        name = gmService.replaceName(name);
        String boo = strings[1];
        Role role = SpringUtils.getRoleService().getOnlinePlayer(name);
        if (role == null) {
            long uid = gmService.getUidByName(name);
            role = SpringUtils.getRoleService().getPlayerBy(uid);
            if (role == null) {
                MessagePusher.pushMessage(aaa, new RespNotifyMiscEx("角色不存在"));
                return true;
            }
        }
        long uid = role.getUid();
        AdminService adminService = SpringUtils.getBean(AdminService.class);
        if (boo.equals("1")) {
            adminService.blockPlayer(role, System.currentTimeMillis() + 90 * TimeUtil.ONE_DAY);
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("你已被封禁!"));
            ServerService serverService = SpringUtils.getBean(ServerService.class);
            serverService.getScheduledExecutorService().schedule(new Runnable() {
                @Override
                public void run() {
                    IoSession session = SessionUtils.getSessionBy(uid);
                    if (session != null) {
                        session.closeNow();
                    }
                }
            }, 3, TimeUnit.SECONDS);
        } else {
            adminService.blockPlayer(role, 0);
            MessagePusher.pushMessage(aaa, new RespNotifyMiscEx("对方封闭已经解开"));
        }
        return true;
    }
}
